package com.qinker.oa.controller;

import com.github.pagehelper.PageInfo;
import com.qinker.oa.exception.GlobalException;
import com.qinker.oa.message.CodeMsg;
import com.qinker.oa.message.Result;
import com.qinker.oa.model.CustomerContactMessage;
import com.qinker.oa.model.CustomerInfo;
import com.qinker.oa.model.User;
import com.qinker.oa.model.vo.CustomerInfoVo;
import com.qinker.oa.service.CustomerInfoService;
import com.qinker.oa.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.List;

@Controller
@Slf4j
public class CustomerInfoController {

    @Autowired
    private CustomerInfoService customerInfoService;

    @Autowired
    private UserService userService;

    private static final int pageSize = 10;

    @GetMapping("/customer/{page}")
    @ResponseBody
    public Result<PageInfo<CustomerInfoVo>> getCustomerInfoList(@PathVariable(name = "page") int page, HttpSession session){
        User loginUser = (User) session.getAttribute("user");
        switch (loginUser.getRole()){
            case "user":
                PageInfo<CustomerInfoVo> pageInfo = customerInfoService.getCustomerInfoListAll(page, pageSize);
                return Result.success(pageInfo);
            case "CUSTO":
                PageInfo<CustomerInfoVo> pageInfo1 = customerInfoService.getCustomerInfoListPerson(page, pageSize, loginUser.getId());
                return Result.success(pageInfo1);
        }
        return Result.success(null);
    }

    @RequestMapping("/customer")
    public String addCustomerInfo(@Valid  CustomerInfo info, HttpSession session){
        try{
            customerInfoService.addCustomerInfo(info, session);
            return "redirect:index.html";
        }catch (Exception e){
            throw new GlobalException(CodeMsg.SERVER_ERROR);
        }
    }

    @GetMapping("/customer/search/{page}")
    @ResponseBody
    public Result<PageInfo<CustomerInfoVo>> search(@PathVariable(name = "page") int page, @RequestParam(required = false)
            String customerNumber, @RequestParam(required = false) String customerName,
                                                   @RequestParam(required = false) String nickname){
        int[] ids = null;
        if (StringUtils.isNotEmpty(nickname) && !nickname.equals("null")){
            ids = userService.getUserByName(nickname);
            if (ids == null || ids.length == 0){
                return Result.success(null);
            }
        }
        PageInfo<CustomerInfoVo> pageInfo = customerInfoService.search(page, pageSize, customerNumber, customerName, ids);
        return Result.success(pageInfo);
    }

    @GetMapping("/customer/info/{id}")
    @ResponseBody
    public Result<List<CustomerContactMessage>> getCustomerMessageById(@PathVariable(name = "id") int aid){
        if (aid == 0){
            return Result.success(null);
        }
        List<CustomerContactMessage> messages = customerInfoService.getMessageById(aid);
        return Result.success(messages);
    }

    @PostMapping("/customer/info/add/{id}")
    @ResponseBody
    public Result<CodeMsg> addMessage(@Valid @RequestBody CustomerContactMessage message, @PathVariable(name = "id") int id){
        if (id == 0){
            return Result.error(CodeMsg.BIND_ERROR);
        }
        try {
            message.setAId(id);
            customerInfoService.insertSchedule(message);
            return Result.successNoData(CodeMsg.SAVE_SUCCESS);
        }catch (GlobalException e){
            log.error(e.getMessage());
            return Result.error(CodeMsg.SERVER_ERROR);
        }
    }

    @DeleteMapping("/customer/{id}")
    @ResponseBody
    public Result<CodeMsg> deleteCustomer(@PathVariable(required = true) int id){
        customerInfoService.deleteCustomer(id);
        return Result.successNoData(CodeMsg.DELETE_SUCCESS);
    }

    @RequestMapping("/customer/edit")
    @ResponseBody
    public Result<CodeMsg> editCustomerInfo(@RequestBody CustomerInfo customerInfo){
        customerInfoService.updateInfoById(customerInfo);
        return Result.successNoData(CodeMsg.SAVE_SUCCESS);
    }
}
